<?php

namespace App\Http\Resources;

use App\Helpers\BaseResource;
use App\Helpers\FileTool;
use App\Logic\UserBuyLogic;

class Doc extends BaseResource
{

    public function toArray($request)
    {
        if ($this->rs_type == 'edit') {
            return $this->open_edit();
        }
        return $this->filterFields([
            'id' => hashid_encode($this->id),
            'title' => $this->title,
            'mode' => $this->mode,
            'name' => $this->title,
            'category' => hashid_encode($this->category_id),
            'category_name' => $this->whenLoaded('category', function () {
                return $this->category->name;
            }),
            'abstract' => $this->abstract,
            'doc_type' => $this->doc_type,
            'money' => (float)$this->money,
            'cover' => \App\Models\Doc::getCover($this),
            'last_update' => $this->whenLoaded('last_update', function () {
                return DocPage::make($this->last_update)->show(['id', 'title', 'updated_format']);
            }),
            'last_article' => $this->whenLoaded('last_article', function () {
                return DocArticle::make($this->last_article)->show(['id', 'title', 'updated_format']);
            }),
            'page_count' => $this->whenLoaded('pages', function () {
                return collect($this->pages)->count();
            }),
            'article_count' => $this->whenLoaded('articles', function () {
                return collect($this->articles)->count();
            }),
            'user' => $this->whenLoaded('user', function () {
                $userRes = User::make($this->user);
                $userRes = $this->get_relate_data('user', $userRes);
                return $userRes;
            }),
            'buy_info' => $this->when($this->ckFields('buy_info'), function () {
                return UserBuyLogic::judgeDoc($this, $this->getUser())->toArray();
            }),
            'tags' => $this->whenLoaded('tags', function () {
                return TagCollection::make($this->tags);
            }),
            'comment_count' => $this->whenLoaded('comments', function () {
                return collect($this->comments)->count();
            }),
            'subscribe_count' => $this->when($this->ckFields('subscribe_count'), function () {
                return collect($this->subscribers)->count();
            }),
            'nav_color' => '#ffffff',// $this->nav_color,
            'font_color' => '#000000',//$this->font_color == 0 ? "#ffffff" : '#000000',
            'listened_count' => $this->whenLoaded('listened', function () {
                return collect($this->listened)->count() + collect($this->article_listened)->count();
            }),
            'time' => $this->created_at->diffForHumans(),
            'created' => (string)$this->created_at,
            'versions' => $this->versions ?? '未知',
            'status' => $this->status,
            'view' => $this->view,
            'share_data' => \App\Models\Doc::getShareData($this, $this->getUser())
        ]);
    }


    private function open_edit()
    {
        return $this->filterFields([
            'id' => hashid_encode($this->id),
            'title' => $this->title,
            'mode' => $this->mode,
            'abstract' => $this->abstract,
            'doc_type' => $this->doc_type,
            'money' => (float)$this->money,
            'resell_enabled' => (boolean)$this->resell_enabled,
            'resell_percent' => (int)$this->resell_percent,
            'category' => \App\Models\Category::getIdArr($this->category),
            'cover' => $this->cover,
            'cover_info' => \App\Models\Doc::getCover($this),
            'cover_id' => hashid_encode($this->cover_id),
            'cover_url' => FileTool::thumb($this->cover),
            'tags' => $this->when($this->ckFields('tags'), function () {
                $tags = $this->tags;
                $ids = [];
                foreach ($tags as $tag) {
                    $ids[] = hashid_encode($tag->id);
                }

                return $ids;
            }),
            'nav_color' => $this->nav_color,
            'font_color' => (boolean)$this->font_color,
            'versions' => $this->versions ?? '未知',
            'status' => $this->status,
        ]);
    }
}
